﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WebAuto
{
    public partial class AppConfigEditor : Form
    {
        private TableLayoutPanel pnlContent = new TableLayoutPanel();
        private Configuration config;
        public AppConfigEditor()
        {
            InitializeComponent();
        }

        private void AppConfigEditor_Load(object sender, EventArgs e)
        {
            try
            {
                config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                int rowCount = config.AppSettings.Settings.Count;

                pnlContent.Dock = DockStyle.Fill;
                pnlContent.AutoScroll = true;
                pnlContent.AutoScrollMargin = new Size(1, 1);
                pnlContent.AutoScrollMinSize = new Size(1, 1);
                pnlContent.RowCount = rowCount;
                pnlContent.ColumnCount = 2;
                for (int i = 0; i < pnlContent.ColumnCount; i++)
                {
                    pnlContent.ColumnStyles.Add(new ColumnStyle());
                }
                pnlContent.ColumnStyles[0].Width = 5;
                pnlContent.ColumnStyles[1].Width = 100;
                this.panel1.Controls.Add(pnlContent);

                int iCount = 0;
                foreach (var setting in config.AppSettings.Settings.AllKeys)
                {
                    pnlContent.RowStyles.Add(new RowStyle(SizeType.Absolute, 20));

                    Label lblTitle = new Label();
                    lblTitle.Name = "Line_BP_" + iCount;
                    lblTitle.Text = setting;
                    lblTitle.TabIndex = (iCount*2);
                    lblTitle.Margin = new Padding(0);
                    lblTitle.Dock = DockStyle.Fill;
                    lblTitle.Width = 200;
                    lblTitle.ForeColor = Color.Black;
                    pnlContent.Controls.Add(lblTitle, 0, iCount);



                    TextBox txtValue = new TextBox();
                    txtValue.Name = "SettingValue_" + setting;
                    txtValue.TabIndex = (iCount*2) + 1;
                    txtValue.Margin = new Padding(0);
                    txtValue.Dock = DockStyle.Fill;
                    txtValue.BorderStyle = BorderStyle.FixedSingle;
                    txtValue.Text = ConfigurationManager.AppSettings[setting];

                    pnlContent.Controls.Add(txtValue, 2, iCount);

                    iCount++;
                }
                label1.Text = "";
            }catch(Exception ex)
            {

                label1.Text = "Error loading the config file."+ex.Message;
            }


        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            //var txt2 = this.Controls.Find(txtName, true);
            var result =
                MessageBox.Show(
                    "Are you sure you want to update the config settings.\nN.B:Incorrect settings may have adverse results in the BunnyChow application?",
                    "Confirm save", MessageBoxButtons.YesNo);

            if (result == DialogResult.Yes)
            {
                try
                {
                    foreach (Control ctl in pnlContent.Controls)
                    {

                        if (ctl is TextBox)
                        {
                            var settingCtrl = (TextBox) ctl;
                            if (settingCtrl.Name.Contains("SettingValue_"))
                            {
                                string settingName = settingCtrl.Name.Replace("SettingValue_", "");
                                string settingValue = settingCtrl.Text;
                                config.AppSettings.Settings[settingName].Value = settingValue;
                                config.Save(ConfigurationSaveMode.Modified);
                            }

                        }

                    }

                    ConfigurationManager.RefreshSection("appSettings");
                    MessageBox.Show(
                        "Saved successfully.You must restart this app to have the new settings to take effect.");
                    this.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error saving to app.config.Try again or try editing manually.\n" + ex.Message);

                }
            }
        }
    }
}
